<template>
    <!-- 根据部门查询部门下的人员 -->
    <a-select v-bind="$attrs" v-on="$listeners" v-model="currentValue" allowClear>
        <a-spin v-if="fetching" slot="notFoundContent" size="small" />
        <a-select-option v-else v-for="i in employees" :key="i.id" :value="i.id">{{
            i.name
        }}</a-select-option>
    </a-select>
</template>
<script>
import { getEmployees } from '@/api/businessAffairs/revenueAndCost'
export default {
    inheritAttrs: false,
    props: {
        // v-model绑定的数据
        value: {
            type: [Array, String],
        },
        // 需要联动的表单名，
        form: {
            type: Object,
            default: function () {
                return {}
            },
        },
        // 需要联动的表单名
        keyValue: {
            type: String,
            default: function () {
                return 'deptNameId'
            },
        },
        // 联动表单值改变时是否清空当前表单值
        isClear: {
            type: Boolean,
            default() {
                return true
            },
        },
    },
    data() {
        return {
            fetching: false,
            employees: [],
        }
    },
    methods: {
        getEmployees(params) {
            this.fetching = true
            let data = {
                pageSize: 100,
                pageNumber:1,
                orgId:params.query.branchComIds[0]
                // nameLike:'',
            }
            getEmployees(data).then((res) => {
               
              this.fetching = false
                    // console.log(res)
                    if (res.status === 200) {
                        this.employees = res.data.list
                    } else {
                        this.$message.error('err:', res.message)
                    }
            })
        },


       
    },
    computed: {
        // 需要联动的表单值
        attr() {
            return this.form[this.keyValue]
        },
        // 转换绑定的选中数据，与v-model同步
        currentValue: {
            get: function () {
                return this.value
            },
            set: function (value) {
                this.$emit('input', value)
            },
        },
    },
    mounted() {
        let val = this.attr
        val && this.getEmployees({ query: { branchComIds: [val] } })
    },
    watch: {
        // 监听联动表单值重置项目列表
        attr(val) {
            // 清空表单
            if (this.isClear) {
                this.currentValue = ''
            }
            // 重置列表
            val && this.getEmployees({ query: { branchComIds: [val] } })
        },
    },
}
</script>
